﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Threading;
using System.Diagnostics;

namespace server.app
{
    public class FindClassType : comm.IFindClassType
    {
        public static Type GetType(string classType)
        {
            var asl = typeof(DataManager).Assembly;
            return asl.GetType(asl.FullName.Split(',')[0] + ".lib." + classType);
        }

        public Type FindLibType(string classType)
        {
            return GetType(classType);
        }
        public string[] GetAllLibType()
        {
            var asl = typeof(DataManager).Assembly;
            var findName = asl.FullName.Split(',')[0] + ".lib";
            var types = asl.GetTypes();
            var list = new List<string>();
            foreach (var type in types)
            {
                if (type.FullName.IndexOf(findName) == 0)
                {
                    list.Add(type.Name);
                }
            }
            return list.ToArray();
        }
    }

    public sealed class World
    {
        public comm.DefineFile DefineFile { get; private set; }
        public DataManager DataManager { get; private set; }
        public ObjectManager ObjectManager { get; private set; }
        public PlayerManager PlayerManager { get; private set; }
        public SpaceManager SpaceManager { get; private set; }
        public StarManager StarManager { get; private set; }
        public ItemManager ItemManager { get; private set; }
        public HeroManager HeroManager { get; private set; }
        public UnitManager UnitManager { get; private set; }
        public BackpackManager BackpackManager { get; private set; }

        public World()
        {
            var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "../bin_data_define");
            DefineFile = new comm.DefineFile(path, new FindClassType());
            DataManager = new DataManager(this, "Data Source=localhost;Database=db1;Uid=sa");
            SpaceManager = new SpaceManager(this);
            ObjectManager = new ObjectManager(this);
            BackpackManager = new app.BackpackManager(this);
            ItemManager = new app.ItemManager(this);
            StarManager = new app.StarManager(this);
            HeroManager = new app.HeroManager(this);
            UnitManager = new app.UnitManager(this);
            PlayerManager = new PlayerManager(this);

            DataManager.InitAllTable();
            StarManager.LoadAllStar();

            //var ss = DefineFile.GetAllViewNValue(comm.objst.NValueViewType.HERO_OTHRE_PLAYER_VIEW);
            //Console.WriteLine(comm.JsonSerializer.Serialize(ss));

            //var star = StarManager.GetStar("1");
            //Console.WriteLine(comm.JsonSerializer.Serialize(star.GetAllSpaceInfo()));

            //BackpackManager.CreackBackpack("test1");

            //var ids1 = ItemManager.CreateItem("test1", new comm.objst.CreateItem
            //{
            //    ItemId = "Hero头盔1",
            //    Count = 1
            //});
            //var ids2 = ItemManager.CreateItem("test1", new comm.objst.CreateItem
            //{
            //    ItemId = "Hero红药水1",
            //    Count = 100
            //});
            //Console.WriteLine(comm.JsonSerializer.Serialize(ids1));
            //Console.WriteLine(comm.JsonSerializer.Serialize(ids2));

            //Console.WriteLine(ItemManager.ItemToBackpack("test1", ids1));
            //Console.WriteLine(ItemManager.ItemToBackpack("test1", ids2));


            //Console.WriteLine(comm.JsonSerializer.Serialize(BackpackManager.GetAllBackpack("test1")));


            //Console.WriteLine("CreateUnit() " + UnitManager.CreateUnit("test1", new comm.objst.CreateUnit
            //{
            //    Level = 10,
            //    Count = 20,
            //    UnitId = "UnitBlade"
            //}));

            //Console.WriteLine("units:" + comm.JsonSerializer.Serialize(UnitManager.GetAllUnit("test1")));

            Console.WriteLine("CreateHero() " + HeroManager.CreateHero(new comm.objst.CreateHero
            {
                FactionId = "1001",
                HeroId = "Hero1",
                Nickname = "jawsbai",
                StarId = "1",
                UserId = "test1"
            }));
        }
    }
}
